package com.lindroy.androidplayer.ui.adapter

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import com.google.android.flexbox.*
import com.lindroy.androidplayer.base.adapter.BaseListAdapter
import com.lindroy.androidplayer.base.adapter.BasePagingAdapter
import com.lindroy.androidplayer.databinding.KnowledgeSystemItemBinding
import com.lindroy.androidplayer.logic.model.bean.TabBean
import com.lindroy.androidplayer.viewmodels.KnowledgeItemViewModel
import com.lindroy.androidplayer.viewmodels.KnowledgeSystemViewModel

/**
 * @author Lin
 * @date 2022/1/24
 * @function 体系列表适配器
 */
class KnowledgeSystemAdapter(private val owner: LifecycleOwner) :
    BasePagingAdapter<KnowledgeSystemViewModel, KnowledgeSystemItemBinding>(owner) {

    @SuppressLint("ClickableViewAccessibility")
    override fun KnowledgeSystemItemBinding.onBindViewHolder(
        item: KnowledgeSystemViewModel?,
        position: Int
    ) {
        if (item == null) {
            return
        }
        vm = item
        rvKnowledge.layoutManager = FlexboxLayoutManager(root.context).apply {
            flexWrap = FlexWrap.WRAP
            flexDirection = FlexDirection.ROW
            justifyContent = JustifyContent.FLEX_START
            alignItems = AlignItems.CENTER
        }

        rvKnowledge.adapter = KnowledgeIndexAdapter(owner).apply {
            setData(item.items.map {
                KnowledgeItemViewModel(
                    id = it.id,
                    name = it.name,
                    tabList = item.items.map { child ->
                        TabBean(
                            child.id,
                            child.name,
                            false,
                            item.id,
                            item.name
                        )
                    },
                    itemClickAction = item.childItemClickAction
                )
            })
        }
        //解决内层 RV 点击空白位置外层点击事件不响应
        rvKnowledge.setOnTouchListener { v, event ->
            root.onTouchEvent(event)
            false
        }
    }
}